package com.lrkj.property.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.lrkj.common.constant.PropertyConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.waitress.domain.RoomCustom;
import com.lrkj.waitress.service.IRoomCustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.property.mapper.RoomAuthMapper;
import com.lrkj.property.domain.RoomAuth;
import com.lrkj.property.service.IRoomAuthService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 房屋认证审批Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-08-02
 */
@Service
public class RoomAuthServiceImpl implements IRoomAuthService
{
    @Autowired
    private RoomAuthMapper roomAuthMapper;

    @Autowired
    private IRoomCustomService roomCustomService;

    /**
     * 查询房屋认证审批
     *
     * @param id 房屋认证审批ID
     * @return 房屋认证审批
     */
    @Override
    public RoomAuth selectRoomAuthById(String id)
    {
        return roomAuthMapper.selectRoomAuthById(id);
    }

    /**
     * 查询房屋认证审批列表
     *
     * @param roomAuth 房屋认证审批
     * @return 房屋认证审批
     */
    @Override
    public List<RoomAuth> selectRoomAuthList(RoomAuth roomAuth)
    {
        return roomAuthMapper.selectRoomAuthList(roomAuth);
    }

    /**
     * 新增房屋认证审批
     *
     * @param roomAuth 房屋认证审批
     * @return 结果
     */
    @Override
    public int insertRoomAuth(RoomAuth roomAuth)
    {
        roomAuth.setCreateTime(DateUtils.getNowDate());
        roomAuth.setId(IdUtil.simpleUUID());
        return roomAuthMapper.insertRoomAuth(roomAuth);
    }

    /**
     * 修改房屋认证审批
     *
     * @param roomAuth 房屋认证审批
     * @return 结果
     */
    @Override
    public int updateRoomAuth(RoomAuth roomAuth)
    {
        roomAuth.setUpdateTime(DateUtils.getNowDate());
        return roomAuthMapper.updateRoomAuth(roomAuth);
    }

    /**
     * 批量删除房屋认证审批
     *
     * @param ids 需要删除的房屋认证审批ID
     * @return 结果
     */
    @Override
    public int deleteRoomAuthByIds(String[] ids)
    {
        return roomAuthMapper.deleteRoomAuthByIds(ids);
    }

    /**
     * 删除房屋认证审批信息
     *
     * @param id 房屋认证审批ID
     * @return 结果
     */
    @Override
    public int deleteRoomAuthById(String id)
    {
        return roomAuthMapper.deleteRoomAuthById(id);
    }

    @Override
    @Transactional
    public int roomAuthApprove(RoomAuth roomAuth) {
        RoomAuth oldRoomAuth = roomAuthMapper.selectRoomAuthById(roomAuth.getId());
        oldRoomAuth.setUpdateTime(DateUtils.getNowDate());
        if(roomAuth.getStatus().equals(PropertyConstants.room_auth_status_20)){
            oldRoomAuth.setStatus(PropertyConstants.room_auth_status_20);
            //添加家庭成员
            RoomCustom roomCustom = new RoomCustom();
            roomCustom.setRoomId(roomAuth.getRoomId());
            roomCustom.setCustomName(roomAuth.getCustomName());
            roomCustom.setCellPhoneNumber(roomAuth.getCellPhoneNumber()+"");
            roomCustom.setIdCard(roomAuth.getIdCard());
            roomCustom.setWrVerify(PropertyConstants.WR_VERIFY_Y);
            roomCustom.setWrDefault(PropertyConstants.WR_VERIFY_N);
            roomCustom.setOwnerType(roomAuth.getOwnerType());
            roomCustom.setRelationType(roomAuth.getRelationType());
            roomCustom.setCustomId(roomAuth.getCustomId());
            roomCustom.setAppUserId(roomAuth.getAppUserId());
            roomCustomService.doSavaOrUpdate(roomCustom);
        }else if(roomAuth.getStatus().equals(PropertyConstants.room_auth_status_30)){
            //拒绝
            oldRoomAuth.setStatus(PropertyConstants.room_auth_status_30);
            oldRoomAuth.setRemark(roomAuth.getRemark());
        }
        return roomAuthMapper.updateRoomAuth(oldRoomAuth);
    }

    /**
     * 批量新增房屋认证审批信息
     *
     * @param roomAuths 房屋认证审批对象
     */
    public int batchAddData(List<RoomAuth> roomAuths)
    {
           return roomAuthMapper.batchAddData(roomAuths);
    }
}
